
package com.epfresh.supplier.adapter;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.epfresh.api.utils.DateUtil;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.Groupon;

import java.util.Timer;
import java.util.TimerTask;

import multitype.ItemViewBinder;
import multitype.MultiTypeAdapter;
import multitype.OnRecyclerViewItemClickListener;

public class HomeBuyerCityItemViewBinder
        extends ItemViewBinder<Groupon, HomeBuyerCityItemViewBinder.ViewHolder> implements View.OnClickListener {
    public interface OnBuyerListener {
        void onBuyer(View view, int position, Groupon data);
    }


    OnBuyerListener onBuyerListener;
    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;


    public OnBuyerListener getOnBuyerListener() {
        return onBuyerListener;
    }

    public void setOnBuyerListener(OnBuyerListener onBuyerListener) {
        this.onBuyerListener = onBuyerListener;
    }

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_home_buyer_city_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onViewDetachedFromWindow(ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
    }


    @Override
    protected void onViewAttachedToWindow(ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
    }

    private Timer timer;
    private boolean isTimerAlive;
    private TimerTask task;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    MultiTypeAdapter adapter = getAdapter();
                    if (adapter != null) {
                        adapter.notifyDataSetChanged();
                    }
                    break;
            }
            super.handleMessage(msg);
        }

    };

    public void timerStart() {
        timerStop();
        timer = new Timer();

        task = new TimerTask() {
            @Override
            public void run() {
                try {


                    if (isTimerAlive) {
                        Message message = new Message();
                        message.what = 1;
                        handler.sendMessage(message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        isTimerAlive = true;

        timer.schedule(task, 0, 1000);
    }

    public void timerStop() {
        isTimerAlive = false;
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (task != null) {
            task.cancel();
            task = null;
        }
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Groupon post) {


        holder.setData(post);

        holder.tvBuyer.setTag(R.id.item_key_position, getPosition(holder));
        holder.tvBuyer.setTag(R.id.item_key_status, post);
        holder.tvBuyer.setOnClickListener(this);
        holder.vProgress.setTag(R.id.item_key_position, getPosition(holder));
        holder.vProgress.setTag(R.id.item_key_status, post);
        holder.vProgress.setOnClickListener(this);
        holder.vTime.setTag(R.id.item_key_position, getPosition(holder));
        holder.vTime.setTag(R.id.item_key_status, post);
        holder.vTime.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);

            if (o instanceof Groupon)
                switch (v.getId()) {
                    case R.id.tv_city_buyer:

                        if (onBuyerListener != null) {
                            onBuyerListener.onBuyer(v, position, (Groupon) o);
                        }
                        break;

                    default:
                        if (onBuyerListener != null) {
                            onBuyerListener.onBuyer(v, position, (Groupon) o);
                        }
                 /*   if (onRecyclerViewItemClickListener != null ) {
                        onRecyclerViewItemClickListener.onItemClick(v, position, o);
                    }*/
                        break;
                }

        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvAddressStart;
        TextView tvAddressEnd;
        TextView tvDate;
        TextView tvBuyer;
        TextView tvAmountBuyer;
        View vLine;
        View vTime;
        View vProgress;

        ProgressBar pb;
        TextView tvHourH;
        TextView tvHourL;
        TextView tvMinutesH;
        TextView tvMinutesL;
        TextView tvSecondH;
        TextView tvSecondL;

        ImageView ivHomeBuyerGoodsState;

        ViewHolder(@NonNull View itemView) {
            super(itemView);

            ivHomeBuyerGoodsState = itemView.findViewById(R.id.iv_home_buyer_goods_state);

            tvAddressStart = itemView.findViewById(R.id.tv_address_1);
            tvAddressEnd = itemView.findViewById(R.id.tv_address_2);
            tvDate = itemView.findViewById(R.id.tv_post_date);
            tvBuyer = itemView.findViewById(R.id.tv_city_buyer);
            pb = itemView.findViewById(R.id.pb);
            tvAmountBuyer = itemView.findViewById(R.id.tv_buyer_amount);
            vLine = itemView.findViewById(R.id.rl_line);

            tvHourH = itemView.findViewById(R.id.tv_num_hour_0);
            tvHourL = itemView.findViewById(R.id.tv_num_hour_1);
            tvMinutesH = itemView.findViewById(R.id.tv_num_minute_0);
            tvMinutesL = itemView.findViewById(R.id.tv_num_minute_1);
            tvSecondH = itemView.findViewById(R.id.tv_num_second_0);
            tvSecondL = itemView.findViewById(R.id.tv_num_second_1);
            vTime = itemView.findViewById(R.id.rl_time);
            vProgress = itemView.findViewById(R.id.rl_progress);
        }

        void setTime(long timeMills) {
            String[] times = DateUtil.getTimeLongToStrings(timeMills);
            if (times != null) {
                tvHourH.setText(times[0]);
                tvHourL.setText(times[1]);
                tvMinutesH.setText(times[2]);
                tvMinutesL.setText(times[3]);
                tvSecondH.setText(times[4]);
                tvSecondL.setText(times[5]);
            } else {
                tvHourH.setText(0);
                tvHourL.setText(0);
                tvMinutesH.setText(0);
                tvMinutesL.setText(0);
                tvSecondH.setText(0);
                tvSecondL.setText(0);
            }
        }

        void setData(Groupon post) {

//            if ("NOREACHQTY".equals(post.getWatermark())) {
//                ivHomeBuyerGoodsState.setImageResource(R.mipmap.on_group_no_full);
//                ivHomeBuyerGoodsState.setVisibility(View.VISIBLE);
//            } else if ("CANDELIVERY".equals(post.getWatermark())) {
//                ivHomeBuyerGoodsState.setImageResource(R.mipmap.on_group_full);
//                ivHomeBuyerGoodsState.setVisibility(View.VISIBLE);
//            } else {
//                ivHomeBuyerGoodsState.setVisibility(View.GONE);
//            }

            tvAddressStart.setText(post.getBaseCityName());
            tvAddressEnd.setText(post.getAgencyCityName());

            pb.setProgress(post.getPercentPage());

            tvAmountBuyer.setText(post.getSumUpCount());
            tvDate.setText("预计" + post.getExpectedDeliverTime() + "可发货");

            if (post.isLast()) {
                vLine.setVisibility(View.GONE);
            } else {
                vLine.setVisibility(View.VISIBLE);
            }
            setTime(post.getReminderTime());
        }
    }
}
